[?php include_stylesheets_for_form($form) ?]
[?php include_javascripts_for_form($form) ?]
[?php use_helper('Javascript') ?]

<div class="sf_admin_form">
  <?php if (sfProjectConfiguration::getActive()->isDebug()): ?>
  [?php if (count($unrenderized = $form->getUnrenderizedFields()) > 0): ?]
    <div class="sf_warning">
      <span class="expander">
        [?php echo link_to_function("[".__('show', array(), 'sf_admin')."]", "$('warning_unrenderized').blindDown(); $(this).hide(); $('unrenderized_hider').show();", array('id' => 'unrenderized_shower')) ?]
        [?php echo link_to_function("[".__('hide', array(), 'sf_admin')."]", "$('warning_unrenderized').blindUp(); $(this).hide(); $('unrenderized_shower').show();", array('id' => 'unrenderized_hider', 'style' => 'display: none')) ?]
      </span>
      [?php echo __("Some fields that haven't been unsetted in the form are not being rendered. This message is only shown in development environment as a warning.", array(), 'sf_admin') ?]
    </div>
    <div class="warning_detail" id="warning_unrenderized" style="display: none">
      <ul>
        [?php foreach ($unrenderized as $name): ?]
          <li>[?php echo __($form[$name]->renderLabelName()).' ('.$name.')' ?]</li>
        [?php endforeach ?]
      </ul>
    </div>
  [?php endif ?]
  <?php endif ?>

  [?php echo form_tag_for($form, isset($form_action) && !is_null($form_action)? '<?php echo $this->params['route_prefix'] ?>/'.$form_action : '@<?php echo $this->params['route_prefix'] ?>', array('id' => 'sf_admin_edit_form', 'onsubmit' => "needs_to_confirm = false; return disableFurtherSubmit(this)")) ?]
    [?php echo $form->renderHiddenFields() ?]

    [?php if ($form->hasGlobalErrors()): ?]
      [?php echo $form->renderGlobalErrors() ?]
    [?php endif; ?]

<?php if (isset($this->params['summarize_errors']) && $this->params['summarize_errors']): ?>
    [?php if ($form->hasErrors()): ?]
      <ul class="error_list">
      [?php foreach ($form->getErrorSchema()->getNamedErrors() as $name => $error): ?]
				[?php $error = ($form[$name]->getError() instanceof sfValidatorError) ? __($form[$name]->getError()->getMessageFormat(), $form[$name]->getError()->getArguments()) : __($form[$name]->getError()) ?]
      	<li>[?php echo __($form[$name]->renderLabelName()) ?]: [?php echo $error ?]</li>
      [?php endforeach; ?]
      </ul>
    [?php endif; ?]
<?php endif; ?>

<?php if (isset($this->params['form_actions_on_top']) && $this->params['form_actions_on_top']): ?>
    [?php include_partial('<?php echo $this->getModuleName() ?>/form_actions', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?]
    
<?php endif; ?>

    [?php $iterateOver = method_exists($form, 'getFieldsets')? $form->getFieldsets() : $form ?]
    [?php foreach ($iterateOver as $fieldset => $fields): ?]
      [?php include_partial('<?php echo $this->getModuleName() ?>/form_fieldset', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>, 'form' => $form, 'fields' => $fields, 'fieldset' => $fieldset)) ?]
    [?php endforeach; ?]

    [?php include_partial('<?php echo $this->getModuleName() ?>/form_actions', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?]
    [?php echo method_exists($form, 'getChildPostRenderJavascripts')? javascript_tag($form->getChildPostRenderJavascripts()) : '' ?]
  </form>
</div>

<?php if (isset($this->params['confirm_on_close']) && $this->params['confirm_on_close']): ?>
<script type="text/javascript">
//<![CDATA[
  var needs_to_confirm = true;

  window.onbeforeunload = function (event) {
    if (needs_to_confirm)
    {
      return "[?php echo __('Are you sure you want to close this window? By doing so, any unsaved work will be lost.', array(), 'sf_admin') ?]";
    }
  };
//]]>
</script>
<?php endif; ?>
